
package com.cnc.tag;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.ConcurrentModificationException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.util.ResponseUtils;
import com.longtop.powerweb.taglib.BaseTag;



public class TreeTag extends BaseTag
{

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getArrayName()
    {
        return arrayName;
    }

    public void setArrayName(String arrayName)
    {
        this.arrayName = arrayName;
    }

    public boolean isAutoCollapse()
    {
        return autoCollapse;
    }

    public void setAutoCollapse(boolean autoCollapse)
    {
        this.autoCollapse = autoCollapse;
    }

    public int getLookAhead()
    {
        return lookAhead;
    }

    public void setLookAhead(int lookAhead)
    {
        this.lookAhead = lookAhead;
    }

    public String getCaptionBgColor()
    {
        return captionBgColor;
    }

    public void setCaptionBgColor(String captionBgColor)
    {
        this.captionBgColor = captionBgColor;
    }

    public String getLinkStyle()
    {
        return linkStyle;
    }

    public void setLinkStyle(String linkStyle)
    {
        this.linkStyle = linkStyle;
    }

    public String getDivStyle()
    {
        return divStyle;
    }

    public void setDivStyle(String divStyle)
    {
        this.divStyle = divStyle;
    }

    public boolean isShowPseudoElement()
    {
        return showPseudoElement;
    }

    public void setShowPseudoElement(boolean showPseudoElement)
    {
        this.showPseudoElement = showPseudoElement;
    }

    public boolean isUseCheckboxSystem()
    {
        return useCheckboxSystem;
    }

    public void setUseCheckboxSystem(boolean useCheckboxSystem)
    {
        this.useCheckboxSystem = useCheckboxSystem;
    }

    public String getCheckboxSystemImgDir()
    {
        return checkboxSystemImgDir;
    }

    public void setCheckboxSystemImgDir(String checkboxSystemImgDir)
    {
        this.checkboxSystemImgDir = checkboxSystemImgDir;
    }

    public boolean isUseRadioButton()
    {
        return useRadioButton;
    }

    public void setUseRadioButton(boolean useRadioButton)
    {
        this.useRadioButton = useRadioButton;
    }

    public String getRadioButtonName()
    {
        return radioButtonName;
    }

    public void setRadioButtonName(String radioButtonName)
    {
        this.radioButtonName = radioButtonName;
    }

    public String getImageDir()
    {
        return imageDir;
    }

    public void setImageDir(String imageDir)
    {
        this.imageDir = imageDir;
    }

    public int getImageHeight()
    {
        return imageHeight;
    }

    public void setImageHeight(int imageHeight)
    {
        this.imageHeight = imageHeight;
    }

    public boolean isUseFolderIcon()
    {
        return useFolderIcon;
    }

    public void setUseFolderIcon(boolean useFolderIcon)
    {
        this.useFolderIcon = useFolderIcon;
    }

    public boolean isUseLeaf()
    {
        return useLeaf;
    }

    public void setUseLeaf(boolean useLeaf)
    {
        this.useLeaf = useLeaf;
    }

    public boolean isDraggable()
    {
        return draggable;
    }

    public void setDraggable(boolean draggable)
    {
        this.draggable = draggable;
    }

    public String getPanelStyle()
    {
        return panelStyle;
    }

    public void setPanelStyle(String panelStyle)
    {
        this.panelStyle = panelStyle;
    }

    public boolean isUseAutoSequence()
    {
        return useAutoSequence;
    }

    public void setUseAutoSequence(boolean useAutoSequence)
    {
        this.useAutoSequence = useAutoSequence;
    }

    public String getUrl()
    {
        return url;
    }

    public void setUrl(String url)
    {
        this.url = url;
    }

    public String getTarget()
    {
        return target;
    }

    public void setTarget(String target)
    {
        this.target = target;
    }

    public String getDataList()
    {
        return dataList;
    }

    public void setDataList(String dataList)
    {
        this.dataList = dataList;
    }

    public String getIdField()
    {
        return idField;
    }

    public void setIdField(String idField)
    {
        this.idField = idField;
    }

    public String getParentIdField()
    {
        return parentIdField;
    }

    public void setParentIdField(String parentIdField)
    {
        this.parentIdField = parentIdField;
    }

    public String getValueField()
    {
        return valueField;
    }

    public void setValueField(String valueField)
    {
        this.valueField = valueField;
    }

    public String getCaptionField()
    {
        return captionField;
    }

    public void setCaptionField(String captionField)
    {
        this.captionField = captionField;
    }

    public String getUrlField()
    {
        return urlField;
    }

    public void setUrlField(String urlField)
    {
        this.urlField = urlField;
    }

    public String getTargetField()
    {
        return targetField;
    }

    public void setTargetField(String targetField)
    {
        this.targetField = targetField;
    }

    public String getParamField()
    {
        return paramField;
    }

    public void setParamField(String paramField)
    {
        this.paramField = paramField;
    }

    public String getCheckboxValueField()
    {
        return checkboxValueField;
    }

    public void setCheckboxValueField(String checkboxValueField)
    {
        this.checkboxValueField = checkboxValueField;
    }

    public String getTopParentValue()
    {
        return topParentValue;
    }

    public void setTopParentValue(String topParentValue)
    {
        this.topParentValue = topParentValue;
    }

    public String getIconField()
    {
        return iconField;
    }

    public void setIconField(String iconField)
    {
        this.iconField = iconField;
    }

    public String getDivStyleField()
    {
        return divStyleField;
    }

    public void setDivStyleField(String divStyleField)
    {
        this.divStyleField = divStyleField;
    }

    public String getLineStyleField()
    {
        return lineStyleField;
    }

    public void setLineStyleField(String lineStyleField)
    {
        this.lineStyleField = lineStyleField;
    }

    public boolean isUseSystemCombineParam()
    {
        return useSystemCombineParam;
    }

    public void setUseSystemCombineParam(boolean useSystemCombineParam)
    {
        this.useSystemCombineParam = useSystemCombineParam;
    }

    public String getRootCaption()
    {
        return rootCaption;
    }

    public void setRootCaption(String rootCaption)
    {
        this.rootCaption = rootCaption;
    }

    public boolean isShowRootUrl()
    {
        return showRootUrl;
    }

    public void setShowRootUrl(boolean showRootUrl)
    {
        this.showRootUrl = showRootUrl;
    }

    public String getRootUrl()
    {
        return rootUrl;
    }

    public void setRootUrl(String rootUrl)
    {
        this.rootUrl = rootUrl;
    }

    public String getRootTarget()
    {
        return rootTarget;
    }

    public void setRootTarget(String rootTarget)
    {
        this.rootTarget = rootTarget;
    }

    public String getImagePath()
    {
        return imagePath;
    }

    public void setImagePath(String imagePath)
    {
        this.imagePath = imagePath;
    }

    public TreeTag()
    {
        name = "root_tree_" + Math.round(Math.random() * 1000D);
        arrayName = "a_" + Math.round(Math.random() * 1000D);
        autoCollapse = false;
        lookAhead = 2;
        imagePath = "/images/";
        showPseudoElement = false;
        showRootUrl = false;
        useCheckboxSystem = false;
        checkboxSystemImgDir = "winxp";
        useRadioButton = false;
        imageDir = "winxp";
        imageHeight = 16;
        useFolderIcon = true;
        useLeaf = true;
        draggable = false;
        panelStyle = "margin:0px";
        useAutoSequence = true;
        idField = "ID";
        parentIdField = "PARENT_ID";
        useSystemCombineParam = true;
    }

    private List getSubList(List dataList, String parentId)
        throws ConcurrentModificationException, NoSuchMethodException, IllegalAccessException, InvocationTargetException
    {
        List subList = new ArrayList();
        Iterator iter = dataList.iterator();
        String _parentId = "";
        Object _parentObj = null;
        while(iter.hasNext()) 
        {
            Object node = iter.next();
            if(node instanceof HashMap)
                _parentObj = ((HashMap)node).get(parentIdField);
            else
                _parentObj = PropertyUtils.getSimpleProperty(node, parentIdField);
            _parentId = _parentObj != null ? _parentObj.toString() : "";
            if(_parentId.trim().equalsIgnoreCase(parentId.trim()))
                subList.add(node);
        }
        return subList;
    }

    private void initTree(String arrayTree, String parentId)
        throws JspException, ConcurrentModificationException, NoSuchMethodException, IllegalAccessException, InvocationTargetException
    {
        if(lstDataList == null)
            return;
        List subList = getSubList(lstDataList, parentId);
        Iterator iter = subList.iterator();
        if(iter.hasNext())
            resultsValue.append(arrayTree + "= new Array;\n");
        int i = 0;
        boolean isHashMap = false;
        String _id = "";
        String _value = "";
        String _caption = "";
        String _url = "";
        String _target = "";
        String _param = "";
        String _checkboxValue = "";
        String _icon = "";
        String _lineStyle = "";
        String _divStyle = "";
        Object _idObj = null;
        Object _valueObj = null;
        Object _captionObj = null;
        Object _urlObj = null;
        Object _targetObj = null;
        Object _paramObj = null;
        Object _checkboxValueObj = null;
        Object _hintObj = null;
        Object _lineStyleObj = null;
        Object _divStyleObj = null;
        while(iter.hasNext()) 
        {
            resultsValue.append(arrayTree + "[" + i + "] = new Array;\n");
            Object node = iter.next();
            isHashMap = node instanceof HashMap;
            if(isHashMap)
                _idObj = ((HashMap)node).get(idField);
            else
                _idObj = PropertyUtils.getSimpleProperty(node, idField);
            _id = _idObj != null ? _idObj.toString().trim() : "";
            if(!useAutoSequence)
                resultsValue.append(arrayTree + "[" + i + "]['id']='" + _id + "';\n");
            if(valueField == null)
            {
                _value = _id;
            } else
            {
                if(isHashMap)
                    _valueObj = ((HashMap)node).get(valueField);
                else
                    _valueObj = PropertyUtils.getSimpleProperty(node, valueField);
                _value = _valueObj != null ? _valueObj.toString().trim() : "";
            }
            resultsValue.append(arrayTree + "[" + i + "]['value']='" + _value + "';\n");
            if(captionField == null)
            {
                _caption = _value;
            } else
            {
                if(isHashMap)
                    _captionObj = ((HashMap)node).get(captionField);
                else
                    _captionObj = PropertyUtils.getSimpleProperty(node, captionField);
                _caption = _captionObj != null ? _captionObj.toString().trim() : "";
            }
            resultsValue.append(arrayTree + "[" + i + "]['caption']='" + _caption + "';\n");
            if(urlField == null)
            {
                if(url != null)
                    _url = url;
            } else
            {
                if(isHashMap)
                    _urlObj = ((HashMap)node).get(urlField);
                else
                    _urlObj = PropertyUtils.getSimpleProperty(node, urlField);
                _url = _urlObj != null ? _urlObj.toString().trim() : "";
            }
            if(!_url.equals(""))
            {
                if(targetField == null)
                {
                    _target = target != null ? target : "_blank";
                } else
                {
                    if(isHashMap)
                        _targetObj = ((HashMap)node).get(targetField);
                    else
                        _targetObj = PropertyUtils.getSimpleProperty(node, targetField);
                    _target = _targetObj != null ? _targetObj.toString().trim() : "";
                }
                resultsValue.append(arrayTree + "[" + i + "]['target']='" + _target + "';\n");
                if(paramField != null)
                {
                    if(isHashMap)
                        _paramObj = ((HashMap)node).get(paramField);
                    else
                        _paramObj = PropertyUtils.getSimpleProperty(node, paramField);
                    _param = _paramObj != null ? _paramObj.toString().trim() : "";
                }
                if(!_param.equals(""))
                {
                    if(useSystemCombineParam)
                        _param = paramField + "=" + _param;
                    if(_url.indexOf("?") == -1)
                        _url = _url + "?" + _param;
                    else
                        _url = _url + "&" + _param;
                }
                resultsValue.append(arrayTree + "[" + i + "]['url']='" + _url + "';\n");
            }
            if(useCheckboxSystem)
            {
                resultsValue.append(arrayTree + "[" + i + "]['checkboxValue']='" + _value + "';\n");
                if(checkboxValueField != null)
                {
                    if(isHashMap)
                        _checkboxValueObj = ((HashMap)node).get(checkboxValueField);
                    else
                        _checkboxValueObj = PropertyUtils.getSimpleProperty(node, checkboxValueField);
                    _checkboxValue = _checkboxValueObj != null ? _checkboxValueObj.toString().trim() : "";
                    int _isChecked = _checkboxValue.equalsIgnoreCase(_value) ? 2 : 0;
                    resultsValue.append(arrayTree + "[" + i + "]['isChecked']=" + _isChecked + ";\n");
                }
            }
            if(useFolderIcon && iconField != null)
            {
                if(isHashMap)
                    _hintObj = ((HashMap)node).get(iconField);
                else
                    _hintObj = PropertyUtils.getSimpleProperty(node, iconField);
                _icon = _hintObj != null ? _hintObj.toString().trim() : "";
                if(!_icon.equals(""))
                    resultsValue.append(arrayTree + "[" + i + "]['icon']='" + _icon + "';\n");
            }
            if(hintField != null)
            {
                if(isHashMap)
                    _hintObj = ((HashMap)node).get(hintField);
                else
                    _hintObj = PropertyUtils.getSimpleProperty(node, hintField);
                _icon = _hintObj != null ? _hintObj.toString().trim() : "";
                if(!_icon.equals(""))
                    resultsValue.append(arrayTree + "[" + i + "]['hint']='" + _icon + "';\n");
            }
            if(lineStyleField != null)
            {
                if(isHashMap)
                    _lineStyleObj = ((HashMap)node).get(lineStyleField);
                else
                    _lineStyleObj = PropertyUtils.getSimpleProperty(node, lineStyleField);
                _lineStyle = _lineStyleObj != null ? _lineStyleObj.toString().trim() : "";
                if(!_lineStyle.equals(""))
                    resultsValue.append(arrayTree + "[" + i + "]['lineStyle']='" + _lineStyle + "';\n");
            }
            if(divStyleField != null)
            {
                if(isHashMap)
                    _divStyleObj = ((HashMap)node).get(divStyleField);
                else
                    _divStyleObj = PropertyUtils.getSimpleProperty(node, divStyleField);
                _divStyle = _divStyleObj != null ? _divStyleObj.toString().trim() : "";
                if(!_divStyle.equals(""))
                    resultsValue.append(arrayTree + "[" + i + "]['divStyle']='" + _divStyle + "';\n");
            }
            initTree(arrayTree + "[" + i + "]['children']", _id);
            i++;
        }
    }

    public int doStartTag()
        throws JspException
    {
        if(dataList == null || idField == null || parentIdField == null)
            return 0;
        if(captionBgColor == null)
            if(useCheckboxSystem)
                captionBgColor = "transparent";
            else
                captionBgColor = "#ccccff";
        return 1;
    }

    public int doEndTag()
        throws JspException
    {
        HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
        lstDataList = (List)request.getAttribute(dataList);
        lstDataList = ((List) (lstDataList != null ? lstDataList : ((List) (new ArrayList()))));
        String contextPath = request.getContextPath();
        String imgPath = contextPath + imagePath;
        String topParentValue = this.topParentValue != null ? this.topParentValue : "";
        StringBuffer results = new StringBuffer();
        resultsValue = new StringBuffer();
        try
        {
            initTree(arrayName, topParentValue);
        }
        catch(NoSuchMethodException e) { }
        catch(IllegalAccessException e) { }
        catch(InvocationTargetException e) { }
        results.append("<script>\n");
        results.append("var " + arrayName + ";\n");
        if(resultsValue.toString().equals(""))
            resultsValue.append(arrayName + "= new Array;\n");
        results.append(resultsValue.toString());
        results.append("var " + name + ";\n");
        results.append("function init()\n");
        results.append("{ \n");
        results.append(" " + name + "=new Bs_Tree();\n");
        results.append(" " + name + ".autoCollapse = " + autoCollapse + ";\n");
        results.append(" " + name + ".lookAhead = " + lookAhead + ";\n");
        results.append(" " + name + ".captionBgColor = '" + captionBgColor + "';\n");
        if(divStyle != null)
            results.append(" " + name + ".divStyle = '" + divStyle + "';\n");
        if(linkStyle != null)
            results.append(" " + name + ".linkStyle = '" + linkStyle + "';\n");
        if(showPseudoElement && rootCaption != null)
        {
            results.append(" " + name + ".rootCaption = '" + rootCaption + "';\n");
            if(showRootUrl)
            {
                results.append(" " + name + ".showRootUrl = " + showRootUrl + ";\n");
                results.append(" " + name + ".rootUrl ='" + (rootUrl != null ? rootUrl : url) + "';\n");
                results.append(" " + name + ".rootTarget = '" + (rootTarget != null ? rootTarget : target) + "';\n");
            }
            results.append(" " + name + ".setRoot();\n");
        }
        results.append(" " + name + ".showPseudoElement = " + showPseudoElement + ";\n");
        results.append(" " + name + ".useCheckboxSystem = " + useCheckboxSystem + ";\n");
        results.append(" " + name + ".checkboxSystemImgDir='" + imgPath + "tree/checkbox/" + checkboxSystemImgDir + "/';\n");
        results.append(" " + name + ".useRadioButton = " + useRadioButton + ";\n");
        if(radioButtonName != null)
            results.append(" " + name + ".radioButtonName = '" + radioButtonName + "';\n");
        results.append(" " + name + ".imageDir='" + imgPath + "tree/tree/" + imageDir + "/';\n");
        results.append(" " + name + ".imageHeight = " + imageHeight + ";\n");
        results.append(" " + name + ".useFolderIcon = " + useFolderIcon + ";\n");
        results.append(" " + name + ".useLeaf = " + useLeaf + ";\n");
        results.append(" " + name + ".draggable = " + draggable + ";\n");
        results.append(" " + name + ".useAutoSequence = " + useAutoSequence + ";\n");
        results.append(" var status=" + name + ".initByArray(" + arrayName + ");\n");
        results.append(" if (!status) {\n");
        results.append("    var treeHtml = " + name + ".getLastError();\n");
        results.append("    document.getElementById('" + name + "Div').innerHTML = treeHtml;\n");
        results.append(" } else {\n");
        results.append("    " + name + ".drawInto('" + name + "Div');\n");
        results.append(" }\n");
        results.append("}\n");
        results.append("</script>\n");
        results.append("<div id='" + name + "Div'");
        if(panelStyle != null)
            results.append(" style='" + panelStyle + "'");
        results.append("></div>");
        ResponseUtils.write(pageContext, results.toString());
        return super.doEndTag();
    }

    public void release()
    {
        super.release();
        resultsValue = null;
        idField = null;
        parentIdField = null;
    }

    protected String dataList;
    protected String name;
    protected String arrayName;
    protected boolean autoCollapse;
    protected int lookAhead;
    protected String captionBgColor;
    protected String imagePath;
    protected String linkStyle;
    protected String divStyle;
    protected boolean showPseudoElement;
    protected String rootCaption;
    protected boolean showRootUrl;
    protected String rootUrl;
    protected String rootTarget;
    protected boolean useCheckboxSystem;
    protected String checkboxSystemImgDir;
    protected boolean useRadioButton;
    protected String radioButtonName;
    protected String imageDir;
    protected int imageHeight;
    protected boolean useFolderIcon;
    protected boolean useLeaf;
    protected boolean draggable;
    protected String panelStyle;
    protected boolean useAutoSequence;
    protected String url;
    protected String target;
    protected String idField;
    protected String parentIdField;
    protected String valueField;
    protected String captionField;
    protected String urlField;
    protected String hintField;
    protected String targetField;
    protected String paramField;
    protected String checkboxValueField;
    protected String iconField;
    protected String divStyleField;
    protected String lineStyleField;
    protected boolean useSystemCombineParam;
    protected String topParentValue;
    private List lstDataList;
    private StringBuffer resultsValue;
	public String getHintField() {
		return hintField;
	}

	public void setHintField(String hintField) {
		this.hintField = hintField;
	}
}
